package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result addDish(@RequestBody DishDTO dto){
        log.info("新增菜品：{}",dto);
        dishService.addDish(dto);
        //清理缓存
        redisTemplate.delete("dish_" + dto.getCategoryId());
        return Result.success();
    }
    /**
     * 菜品分页查询
     */

    @GetMapping("/page")
    @ApiOperation(value = "菜品分页查询")
    public Result Page(DishPageQueryDTO dto){
        log.info("菜品分页查询：{}",dto);
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询菜品
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询菜品")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        DishDTO dishDTO = dishService.getById(id);
        log.info("查询到的菜品数据：{}",dishDTO);
        return Result.success(dishDTO);

    }
    /**
     * 修改菜品
     */
    @PutMapping
    @ApiOperation(value = "修改菜品")
    public Result update(@RequestBody  DishDTO dto){
        log.info("修改菜品：{}",dto);
        dishService.update(dto);
        //缓存优化
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    /**
     * 批量删除菜品
     */

    @DeleteMapping
    @ApiOperation(value = "批量删除菜品")
    public Result delete(String ids) {
        log.info("批量删除菜品：{}", ids);
        dishService.deleteBatch(ids);
        //缓存优化-将菜品缓存全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据分类id查询菜品")
    public Result getByCategoryId(Integer categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        List<Dish> list = dishService.getByCategoryId(categoryId);
        return Result.success(list);
    }
    /**
     * 菜品的停售起售
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "菜品的停售起售")
    public Result startOrStop(@PathVariable Integer status,Long id) {
        log.info("菜品的停售起售：{}", status);
        dishService.startOrStop(status,id);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }

}
